package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.common.dto.AttachmentDto;
import com.koron.css2.common.service.AbstractExportExcel;
import com.koron.css2.newProjectCenter.Enum.PjInfoProcessStateEnum;
import com.koron.css2.newProjectCenter.bean.query.PjTerminationQueryBean;
import com.koron.css2.newProjectCenter.mapper.PjTerminationMapper;
import com.koron.css2.newProjectCenter.vo.PjTerminationListVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;

import java.util.List;
import java.util.Map;

/**
 * 工程终止查询列表导出
 * 1. 前端传query
 * 2. 数据处理
 * 3. 返回excel文件
 */
public class PjTerminationExport extends AbstractExportExcel {
    @Override
    public AttachmentDto getAttachment(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        PjTerminationQueryBean pjTerminationQueryBean = JsonUtils.objectToPojo(req.getData(), PjTerminationQueryBean.class);
        PjTerminationMapper pjTerminationMapper = factory.getMapper(PjTerminationMapper.class);
        Map<String, String> PPT = Tools.mapDicByCode(factory, "PPT");

        // 1. 查询列表
        List<PjTerminationListVO> list = pjTerminationMapper.selectList(pjTerminationQueryBean);
        for (PjTerminationListVO bean : list) {
            // 2.1 pjType -> pjTypeName
            bean.setPjTypeName(PPT.get(bean.getPjType().toString()));
            // 2.2 processState -> processName
            if (StringUtils.isNotBlank(bean.getProcessState())) {
                bean.setProcessState(Tools.transProcessStatus(bean.getProcessState()));
            }
            // 2.3 nodeCode -> nodeCodeName
            String nodeCode = bean.getNodeCode();
            if (StringUtils.isNotEmpty(nodeCode)) {
                String nodeCodeName = PjInfoProcessStateEnum.getNodeCodeName(nodeCode);
                bean.setNodeCodeName(nodeCodeName);
            }
            // 2.4 billDate -> billDateString
            String billDateString = Tools.formatDateToString(bean.getBillDate());
            bean.setBillDateString(billDateString);
        }
        // 3. 导出excel
        AttachmentDto attachment = new AttachmentDto();
        attachment.setBtlTemplate("PjTerminationExport.btl");
        attachment.setData(list);
        attachment.setFilename("工程终止列表导出");
        attachment.setFileTemplate("PjTerminationExport.xlsx");
        attachment.setFileType("excel");
        return attachment;
    }
}
